import {createRouter, createWebHistory, createWebHashHistory} from 'vue-router'
import HomeView from '../views/HomeView.vue'
import LoginView from '../views/LoginView.vue'
import Register from '../views/RegisterView.vue'

const router = createRouter({
    history: createWebHashHistory(),
    routes: [
        {
            path: '/',
            name: 'home',
            component: HomeView,
            // 路由嵌套
            children: [
                {
                    path: "",
                    name: 'personalCenter',
                    component: () => import('../views/contents/PersonalCenter.vue')
                },
                {
                    path: "adminManage",
                    name: 'adminManage',
                    component: () => import('../views/manage/AdminManage.vue')
                },
                {
                    path: "userManage",
                    name: 'userManage',
                    component: () => import('../views/manage/UserManage.vue')
                },
                {
                    path: "warehouseManage",
                    name: 'warehouseManage',
                    component: () => import('../views/manage/WarehouseManage.vue')
                },
                {
                    path: "goodsTypeManage",
                    name: 'goodsTypeManage',
                    component: () => import('../views/manage/GoodsTypeManage.vue')
                }
            ],
            //路由守卫
            beforeEnter: (to, from) => {
                // reject the navigation
                let token = localStorage.getItem('token')
                if (token) {
                    return true
                } else {
                    return {path: '/login'}
                }
            }
        },
        {
            path: '/login',
            name: 'login',
            component: LoginView,
        },
        {
            path: '/register',
            name: 'register',
            component: Register,
        }
    ],


})

export default router
